package com.example.mansystem.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.mansystem.dto.ChangeInstrumentExamineDto;
import com.example.mansystem.dto.DeleteInDto;
import com.example.mansystem.dto.InstrumentEditDto;
import com.example.mansystem.dto.InstrumentRegisterDto;
import com.example.mansystem.entity.ExamineInfoDO;
import com.example.mansystem.entity.InstrumentExamineDO;
import com.example.mansystem.entity.ProcessMangerDO;
import com.example.mansystem.mapper.ExamineInfoMapper;
import com.example.mansystem.mapper.InstrumentExamineMapper;
import com.example.mansystem.mapper.ProcessMangerMapper;
import com.example.mansystem.vo.ExamineInfo;
import org.activiti.engine.*;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;


/**
 * @author fjl
 */
@Service
public class ActService {
    @Autowired
    RepositoryService repositoryService;
    @Autowired
    RuntimeService runtimeService;
    @Autowired
    TaskService taskService;
    @Resource
    InstrumentExamineMapper instrumentExamineMapper;
    @Resource
    ProcessMangerMapper processMangerMapper;
    @Resource
    ExamineInfoMapper examineInfoMapper;

    public ExamineInfo getListByRoleName(String roleName)
    {

        List<Task> taskList = taskService.createTaskQuery()
                .taskAssignee(roleName)
                .list();

        List<InstrumentExamineDO> res1=new ArrayList<InstrumentExamineDO>();
        List<String> res2=new ArrayList<String>();
        List<String> res3=new ArrayList<String>();
        for(Task task:taskList)
        {
            res2.add(task.getId());
            res1.add(instrumentExamineMapper.selectById((int)taskService.getVariable(task.getId(),"examId")));
            res3.add(task.getName());
        }
        return ExamineInfo.builder().examine(res1).taskId(res2).taskName(res3).build();

    }

    public ExamineInfo getEditList(String username)
    {
        QueryWrapper<InstrumentExamineDO> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(InstrumentExamineDO::getUsername,username);

        List<InstrumentExamineDO> instrumentExamineDOList = instrumentExamineMapper.selectList(wrapper);

        List<InstrumentExamineDO> res1=new ArrayList<InstrumentExamineDO>();
        List<String> res2=new ArrayList<String>();
        List<String> res3=new ArrayList<String>();
        List<String> res4=new ArrayList<String>();
        for(int i=0;i<instrumentExamineDOList.size();i++)
        {
            String processId=instrumentExamineDOList.get(i).getProcessId();

            List<Task> taskList = taskService.createTaskQuery()
                    .processInstanceId(processId)
                    .list();
            if(taskList.size()==0&&instrumentExamineDOList.get(i).getState()==1)
            {
                res1.add(instrumentExamineDOList.get(i));
                res2.add("-1");
                res3.add("完成");
                res4.add(instrumentExamineDOList.get(i).getProcessId());
            }
            else if(taskList.size()==0&&instrumentExamineDOList.get(i).getState()==-1)
            {
                res1.add(instrumentExamineDOList.get(i));
                res2.add("-1");
                res3.add("失败");
                res4.add(instrumentExamineDOList.get(i).getProcessId());
            }
            else
            {
                for(Task task:taskList)
                {
                    res1.add(instrumentExamineDOList.get(i));
                    res2.add(task.getId());
                    res3.add(task.getName());
                    res4.add(task.getProcessInstanceId());
                }
            }
        }
        return ExamineInfo.builder().examine(res1).taskId(res2).taskName(res3).processId(res4).build();
    }

    public void ExaminePass(ChangeInstrumentExamineDto dto)
    {
        int pass=dto.getPass();
        Task task = taskService.createTaskQuery()
                .taskId(dto.getTaskId())
                .singleResult();
        Map<String, Object> map = new HashMap<>();
        map.put("pass",pass);
        if(pass==0)
        {
            InstrumentExamineDO instrumentExamineDO = instrumentExamineMapper.selectById(dto.getId());
            instrumentExamineDO.setErrReason(dto.getErrReason());
            instrumentExamineMapper.updateById(instrumentExamineDO);
        }
        ExamineInfoDO examineInfoDO = ExamineInfoDO.builder().examId(dto.getId()).
                pass(pass).
                username(dto.getUsername()).
                taskName(task.getName()).
                time(dto.getTime()).
                build();
        examineInfoMapper.insert(examineInfoDO);
        taskService.setVariablesLocal(task.getId(), map);
        taskService.complete(task.getId());
    }

    public void notContinueEdit(String processId)
    {
        Task task = taskService.createTaskQuery()
                .processInstanceId(processId)
                .taskName("仪器修改")
                .singleResult();
        Map<String, Object> map = new HashMap<>();
        map.put("edit",0);
        taskService.setVariablesLocal(task.getId(), map);
        taskService.complete(task.getId());
    }

    public void continueEdit(String processId,int money)
    {
        Task task = taskService.createTaskQuery()
                .processInstanceId(processId)
                .taskName("仪器修改")
                .singleResult();
        Map<String, Object> map = new HashMap<>();
        map.put("edit",1);
        map.put("money",money);
        taskService.setVariablesLocal(task.getId(), map);
        taskService.complete(task.getId());
    }

    public String InstrumentRegister(InstrumentRegisterDto dto,int examId)
    {
        LambdaQueryWrapper<ProcessMangerDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ProcessMangerDO::getType,1).eq(ProcessMangerDO::getState,1);
        ProcessMangerDO processMangerDO = processMangerMapper.selectOne(wrapper);
        String processDefinitionKey = processMangerDO.getProcessKey();
        Map<String, Object> map = new HashMap<>();

        map.put("username",dto.getRegisterName());
        map.put("money",dto.getJE());
        map.put("techlogy",1);
        map.put("place",1);
        map.put("admin",0);
        map.put("economic",0);
        map.put("examId",examId);
        map.put("type",1);
        map.put("pass",1);
        ProcessInstance instance = runtimeService.startProcessInstanceByKey(processDefinitionKey, map);

        return instance.getId();
    }

    public String InstrumentDelete(DeleteInDto dto,int examId)
    {
        LambdaQueryWrapper<ProcessMangerDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ProcessMangerDO::getType,3).eq(ProcessMangerDO::getState,1);
        ProcessMangerDO processMangerDO = processMangerMapper.selectOne(wrapper);
        String processDefinitionKey = processMangerDO.getProcessKey();
        Map<String, Object> map = new HashMap<>();
        map.put("username",dto.getUsername());
        map.put("type",3);
        map.put("examId",examId);
        map.put("pass",1);
        ProcessInstance instance = runtimeService.startProcessInstanceByKey(processDefinitionKey, map);

        return instance.getId();
    }

    public String InstrumentEdit(InstrumentEditDto dto,int examId)
    {
        LambdaQueryWrapper<ProcessMangerDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ProcessMangerDO::getType,2).eq(ProcessMangerDO::getState,1);
        ProcessMangerDO processMangerDO = processMangerMapper.selectOne(wrapper);
        String processDefinitionKey = processMangerDO.getProcessKey();
        Map<String, Object> map = new HashMap<>();
        map.put("username",dto.getUsername());
        map.put("type",2);
        map.put("examId",examId);
        map.put("pass",1);
        ProcessInstance instance = runtimeService.startProcessInstanceByKey(processDefinitionKey, map);

        return instance.getId();
    }

    /**
     * 启动流程实例
     */
    public ProcessInstance startProcess(String processDefinitionKey,String businessKey,Map<String,Object> map){
        return runtimeService.startProcessInstanceById(processDefinitionKey, businessKey, map);
    }
}

